package com.chuangjiangx.member.stored.ddd.query;

import com.chuangjiangx.commons.CJBeanUtils;
import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.domain.shared.model.PayEntry;
import com.chuangjiangx.member.stored.ddd.dal.dto.*;
import com.chuangjiangx.member.stored.ddd.dal.mapper.RechargeRuleCountDalMapper;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrSourceTerminal;
import com.chuangjiangx.member.stored.ddd.dal.condition.*;
import com.chuangjiangx.member.stored.ddd.query.dto.*;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.List;

/**
 * 会员储值分析统计
 *
 * @author leeXyan
 * @Date 2017/12/18 16:46
 **/
@Component
public class MbrRechargeRuleCountQuery {

    private final RechargeRuleCountDalMapper rechargeRuleCountDalMapper;

    @Autowired
    public MbrRechargeRuleCountQuery(RechargeRuleCountDalMapper rechargeRuleCountDalMapper) {
        this.rechargeRuleCountDalMapper = rechargeRuleCountDalMapper;
    }


    /**
     * 会员储值分析-按储值规则-图表-商户权限
     *
     * @param condition 参数
     * @return list
     */
    public List<RechargeRuleCountChartDTO> getRechargeRuleCountChartForMerchant(RechargeRuleCountChartCondition condition) {
        Assert.notNull(condition.getMerchantId(), "商户id不能为空");
        condition.setStoreId(null);
        List<RechargeRuleCountChart> list = rechargeRuleCountDalMapper.getRechargeRuleCountChart(condition);
        return CJBeanUtils.convertCollection(list, RechargeRuleCountChartDTO.class);
    }


    /**
     * 会员储值分析-按储值规则-图表-门店权限
     *
     * @param condition 参数
     * @return list
     */
    public List<RechargeRuleCountChartDTO> getRechargeRuleCountChartForStore(RechargeRuleCountChartCondition condition) {
        Assert.notNull(condition.getMerchantId(), "商户id不能为空");
        Assert.notNull(condition.getStoreId(), "门店id不能为空");
        List<RechargeRuleCountChart> list = rechargeRuleCountDalMapper.getRechargeRuleCountChart(condition);
        return CJBeanUtils.convertCollection(list, RechargeRuleCountChartDTO.class);
    }


    /**
     * 会员储值分析-按储值规则-列表-商户权限
     *
     * @param condition 参数
     * @return list
     */
    public PagingResult<RechargeRuleCountListDTO> getRechargeRuleCountListForMerchant(RechargeRuleCountListCondition condition) {
        Assert.notNull(condition.getMerchantId(), "商户id不能为空");
        condition.setStoreId(null);
        RowBounds rb = new RowBounds(condition.offset(), condition.limit());//getStoredRulesCountListWithRowBounds
        List<RechargeRuleCountList> list = rechargeRuleCountDalMapper.getRechargeRuleCountListWithRowBounds(condition, rb);
        PageInfo<RechargeRuleCountList> pageInfo = new PageInfo<>(list);
        List<RechargeRuleCountListDTO> listDto = CJBeanUtils.convertCollection(list, RechargeRuleCountListDTO.class);
        return new PagingResult<>(pageInfo.getTotal(), listDto);
    }

    /**
     * 会员储值分析-按储值规则-列表-门店权限
     *
     * @param condition 参数
     * @return PagingResult
     */
    public PagingResult<RechargeRuleCountListDTO> getRechargeRuleCountListForStore(RechargeRuleCountListCondition condition) {
        Assert.notNull(condition.getMerchantId(), "商户id不能为空");
        Assert.notNull(condition.getStoreId(), "门店id不能为空");
        RowBounds rb = new RowBounds(condition.offset(), condition.limit());
        List<RechargeRuleCountList> list = rechargeRuleCountDalMapper.getRechargeRuleCountListWithRowBounds(condition, rb);
        PageInfo<RechargeRuleCountList> pageInfo = new PageInfo<>(list);
        List<RechargeRuleCountListDTO> listDto = CJBeanUtils.convertCollection(list, RechargeRuleCountListDTO.class);
        return new PagingResult<>(pageInfo.getTotal(), listDto);
    }


    /**
     * 按照来源统计-图表-商户权限
     *
     * @param condition 参数
     * @return list
     */
    public List<RechargeRuleTerminalCountChartDTO> getRechargeRuleTerminalCountChartForMerchant(RechargeRuleTerminalCountChartCondition condition) {
        Assert.notNull(condition.getMerchantId(), "商户id不能为空");
        condition.setStoreId(null);
        List<RechargeRuleTerminalCountChart> list = rechargeRuleCountDalMapper.getRechargeRuleTerminalCountChart(condition);
        return CJBeanUtils.convertCollection(list, RechargeRuleTerminalCountChartDTO.class, (source, target) -> {
            if (source.getPayTerminal() != null) {
                target.setPayTerminalName(MbrSourceTerminal.getSourceTerminal(source.getPayTerminal()).name);
            }
        });
    }


    /**
     * 按照来源统计-图表-门店权限
     *
     * @param condition 参数
     * @return list
     */
    public List<RechargeRuleTerminalCountChartDTO> getRechargeRuleTerminalCountChartForStore(RechargeRuleTerminalCountChartCondition condition) {
        Assert.notNull(condition.getMerchantId(), "商户id不能为空");
        Assert.notNull(condition.getStoreId(), "门店id不能为空");
        List<RechargeRuleTerminalCountChart> list = rechargeRuleCountDalMapper.getRechargeRuleTerminalCountChart(condition);
        return CJBeanUtils.convertCollection(list, RechargeRuleTerminalCountChartDTO.class, (source, target) -> {
            if (source.getPayTerminal() != null) {
                target.setPayTerminalName(MbrSourceTerminal.getSourceTerminal(source.getPayTerminal()).name);
            }
        });
    }


    /**
     * 按照来源统计-列表-商户权限
     *
     * @param condition
     * @return PagingResult
     */
    public PagingResult<RechargeRuleTerminalCountForListDTO> getRechargeRuleTerminalCountForListForMerchant(RechargeRuleTerminalCountForListCondition condition) {
        Assert.notNull(condition.getMerchantId(), "商户id不能为空");
        condition.setStoreId(null);

        RowBounds rb = new RowBounds(condition.offset(), condition.limit());
        List<RechargeRuleTerminalCountForList> list = rechargeRuleCountDalMapper.getRechargeRuleTerminalCountForList(condition, rb);
        PageInfo<RechargeRuleTerminalCountForList> pageInfo = new PageInfo<>(list);
        List<RechargeRuleTerminalCountForListDTO> listDto = CJBeanUtils.convertCollection(list, RechargeRuleTerminalCountForListDTO.class, (source, target) -> {
            if (source.getPayTerminal() != null) {
                target.setPayTerminalName(MbrSourceTerminal.getSourceTerminal(source.getPayTerminal()).name);
            }
        });
        return new PagingResult<>(pageInfo.getTotal(), listDto);
    }


    /**
     * 按照来源统计-列表-门店权限
     *
     * @param condition
     * @return PagingResult
     */
    public PagingResult<RechargeRuleTerminalCountForListDTO> getRechargeRuleTerminalCountForListForStore(RechargeRuleTerminalCountForListCondition condition) {
        Assert.notNull(condition.getMerchantId(), "商户id不能为空");
        Assert.notNull(condition.getStoreId(), "门店id不能为空");
        RowBounds rb = new RowBounds(condition.offset(), condition.limit());
        List<RechargeRuleTerminalCountForList> list = rechargeRuleCountDalMapper.getRechargeRuleTerminalCountForList(condition, rb);
        PageInfo<RechargeRuleTerminalCountForList> pageInfo = new PageInfo<>(list);
        List<RechargeRuleTerminalCountForListDTO> listDto = CJBeanUtils.convertCollection(list, RechargeRuleTerminalCountForListDTO.class, (source, target) -> {
            if (source.getPayTerminal() != null) {
                target.setPayTerminalName(MbrSourceTerminal.getSourceTerminal(source.getPayTerminal()).name);
            }
        });
        return new PagingResult<>(pageInfo.getTotal(), listDto);
    }

    /**
     * 按储值规则详情-商户权限
     *
     * @param condition 参数
     * @return
     */
    public PagingResult<RechargeRuleCountDetailListDTO> getRechargeRuleCountDetailListForMerchant(RechargeRuleCountDetailListCondition condition) {
        Assert.notNull(condition.getMerchantId(), "商户id不能为空");
        condition.setStoreId(null);
        RowBounds rb = new RowBounds(condition.offset(), condition.limit());
        List<RechargeRuleCountDetailList> list = rechargeRuleCountDalMapper.getRechargeRuleCountDetailList(condition, rb);

        PageInfo<RechargeRuleCountDetailList> pageInfo = new PageInfo<>(list);
        List<RechargeRuleCountDetailListDTO> listDto = getRechargeRuleCountDetailListDTOS(list);
        listDto.forEach(rechargeRuleCountDetailListDTO -> {
            BigDecimal availableBalance = rechargeRuleCountDalMapper.getAvailableBalance(rechargeRuleCountDetailListDTO.getId());
            if (availableBalance != null) {
                rechargeRuleCountDetailListDTO.setAvailableBalance(availableBalance);
            }
        });
        return new PagingResult<>(pageInfo.getTotal(), listDto);
    }


    /**
     * 按储值规则详情-门店权限
     *
     * @param condition
     * @return
     */
    public PagingResult<RechargeRuleCountDetailListDTO> getRechargeRuleCountDetailListForStore(RechargeRuleCountDetailListCondition condition) {
        Assert.notNull(condition.getMerchantId(), "商户id不能为空");
        Assert.notNull(condition.getStoreId(), "门店id不能为空");
        RowBounds rb = new RowBounds(condition.offset(), condition.limit());
        List<RechargeRuleCountDetailList> list = rechargeRuleCountDalMapper.getRechargeRuleCountDetailList(condition, rb);
        PageInfo<RechargeRuleCountDetailList> pageInfo = new PageInfo<>(list);
        List<RechargeRuleCountDetailListDTO> listDto = getRechargeRuleCountDetailListDTOS(list);
        return new PagingResult<>(pageInfo.getTotal(), listDto);
    }

    /**
     * 类型转换
     *
     * @param list 参数
     * @return list
     */
    private List<RechargeRuleCountDetailListDTO> getRechargeRuleCountDetailListDTOS(List<RechargeRuleCountDetailList> list) {
        return CJBeanUtils.convertCollection(list, RechargeRuleCountDetailListDTO.class, (source, target) -> {
            if (source.getPayTerminal() != null) {
                target.setPayTerminalName(MbrSourceTerminal.getSourceTerminal(source.getPayTerminal()).name);//终端
            }
            if (source.getPayEntry() != null) {
                target.setPayEntryName(PayEntry.getPayEntry(Integer.valueOf(source.getPayEntry())).name);//支付类型
            }
        });
    }


    /***
     * 按来源详情-商户权限
     * @param condition 参数
     * @return PagingResult
     */
    public PagingResult<RechargeRuleTerminalCountDetailListDTO> getRechargeRuleTerminalCountDetailListForMerchant(RechargeRuleTerminalCountDetailListCondition condition) {
        Assert.notNull(condition.getMerchantId(), "商户id不能为空");
        condition.setStoreId(null);
        RowBounds rb = new RowBounds(condition.offset(), condition.limit());
        List<RechargeRuleTerminalCountDetailList> list = rechargeRuleCountDalMapper.getRechargeRuleTerminalCountDetailList(condition, rb);
        PageInfo<RechargeRuleTerminalCountDetailList> pageInfo = new PageInfo<>(list);
        List<RechargeRuleTerminalCountDetailListDTO> listDto = CJBeanUtils.convertCollection(list, RechargeRuleTerminalCountDetailListDTO.class, (source, target) -> {
            if (source.getPayEntry() != null) {
                target.setPayEntryName(PayEntry.getPayEntry(Integer.valueOf(source.getPayEntry())).name);//支付类型
            }
        });
        return new PagingResult<>(pageInfo.getTotal(), listDto);
    }

    /***
     * 按来源详情-门店权限
     * @param condition 参数
     * @return PagingResult
     */
    public PagingResult<RechargeRuleTerminalCountDetailListDTO> getRechargeRuleTerminalCountDetailListForStore(RechargeRuleTerminalCountDetailListCondition condition) {
        Assert.notNull(condition.getMerchantId(), "商户id不能为空");
        Assert.notNull(condition.getStoreId(), "门店id不能为空");
        RowBounds rb = new RowBounds(condition.offset(), condition.limit());
        List<RechargeRuleTerminalCountDetailList> list = rechargeRuleCountDalMapper.getRechargeRuleTerminalCountDetailList(condition, rb);
        PageInfo<RechargeRuleTerminalCountDetailList> pageInfo = new PageInfo<>(list);
        List<RechargeRuleTerminalCountDetailListDTO> listDto = CJBeanUtils.convertCollection(list, RechargeRuleTerminalCountDetailListDTO.class, (source, target) -> {
            if (source.getPayEntry() != null) {
                target.setPayEntryName(PayEntry.getPayEntry(Integer.valueOf(source.getPayEntry())).name);//支付类型
            }
        });
        return new PagingResult<>(pageInfo.getTotal(), listDto);
    }
}
